
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Caching;

// PetShop specific imports

using PetShop.Web.Controls;
using PetShop.Domain.Model;
using PetShop.Application.Facade;
using System.Collections.Generic;
using System.Linq;

namespace PetShop.Web
{
	public class Items : Page
	{
		protected SimplePager items;
		protected NavBar header;
		protected System.Web.UI.WebControls.Label productName;

		private void InitializeComponent() {}

		protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
		{

			items.CurrentPageIndex = e.NewPageIndex;

			// Get the productId from the query string
			string productId = WebComponents.CleanString.InputText(Request["productId"], 50);

			// Array for the data
			List<ItemEntity> itemsByProduct;

			// Check if the data exists in the data cache
			if (Cache[productId] != null)
			{
                itemsByProduct = (List<ItemEntity>)Cache[productId];
			}
			else
			{
				// If the data is not in the cache then fetch the data from the business logic tier
				var fcd = new CatalogFC();
				itemsByProduct = fcd.GetItemsByProduct(productId).ToList();
				// store the output in the data cache with a 12 hour expiry
				Cache.Add(productId, itemsByProduct, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
			}

			// Set the control datasource
			items.DataSource = itemsByProduct;

			// If there is data fetch the product name

		    if (itemsByProduct != null)
		    {
		        var item = itemsByProduct.FirstOrDefault();
		        if (item != null) {productName.Text = item.Product.Name;}
		    }


		    // Bind the data to the the control
			items.DataBind();
		}
	}
}